import { Component } from "@angular/core";
import { NavController, NavParams, AlertController } from "ionic-angular";
import { HttpService } from "../../../providers/http.service";
import { CommonFunc } from "../../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import { InformDetail } from "../../../pages/refresher/inform-detail/inform-detail";
import { NoticeDetail } from "../../../pages/refresher/notice-detail/notice-detail";
import { SignPage } from "../../sign/sign";
import { ApplyRepairDetailPage } from "../../repair-apply-detail/apply-repair-detail";
import { SafeControlDetailPage } from "../../supervision/safe-control-detail/safe-control-detail";
import { appConfig, constant } from "../../../providers/config";
import { RecordCheckPage } from "../record-check/record-check";
import { CheckItem} from "../check-item/check-item";
import { RecordMedia } from "../record-media/record-media";
import {p} from "@angular/core/src/render3";
import {CheckElevator} from "../../maintenance/checkElevator";

@Component({
  templateUrl: "plan-list.html"
})
export class PlanList {
  private title: string; //标题
  private list: any = [];
  private pageSize: number = 10;
  private currentPage: number = 1;
  private currentUserId: any;
  private partyId: any;
  private parentId: any;
  isNotice: boolean = false;
  isRead: any = "0";
  refresher: any;
  userName: any;
  private PLAN_DICT_TYPE: any = [];
  CHECK_STATE: any = [];

  //获取data登录数据
  private dataParm:any;
  // 维保人员的数据
  private maintRecordList:any=[];
  // 单位Id
  public partyType:any;
  // 角色Id
  public roleId:any;
  // 是否显示最后一层
  public isShowNoData:any;

  constructor(
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private httpService: HttpService,
    private storage: Storage,
    private navController: NavController,
    public alertCtrl: AlertController
  ) {
    if (navParams.get("type") == "1") {
      this.isNotice = true;
    }
    this.PLAN_DICT_TYPE = constant.PLAN_DICT_TYPE;
    this.CHECK_STATE = constant.CHECK_STATE;
  }
  ionViewWillEnter() {
    this.pageSize = 10;
    this.currentPage = 1;
    this.query();
  }

  selected() {
    if (this.refresher) {
      this.refresher.enabled = true;
    }
    this.pageSize = 10;
    this.currentPage = 1;
    this.query();
  }
  query() {
    this.list = [];
    let param = {
      pageSize: this.pageSize,
      currentPage: this.currentPage,
      rsponUserName: "",
      checkState: null
    };
    if (this.navParams.get("type") == "1") {
      this.title = "已完成检查";
      param.checkState = "1";
    } else {
      this.title = "未完成检查";
      param.checkState = "0,2";
    }
    console.log(this.storage);

    this.storage.get("user").then(data => {
      // 获取数据
      this.dataParm = data;
      this.partyType = data.partyType;
      this.roleId = data.roleId;

      // 维保查询
      if(this.dataParm.partyType =='1'){

        let userId = this.dataParm.id;
        // 未检查为0
        this.httpService.queryMaintRecord({"userId":userId,"checkRecord":0}).subscribe(data => {
            if (data.status != 200) {
                let toast = this.commonFunc.showInsideErrorToast(data);
                toast.present();
                return;
            }
            this.maintRecordList = data.data;
        });


      }else{
          // 监管查询
          param.rsponUserName = data.userName;
          this.userName = data.userName;
          this.httpService.queryPlanDetail(param).subscribe(data => {
              if (data.status != 200) {
                  let toast = this.commonFunc.showInsideErrorToast(data);
                  toast.present();
                  return;
              }
              this.list = data.data.list;
          });
      }

    });
  }

  //刷新
  doRefresh(refresher) {
    if (!this.refresher) {
      this.refresher = refresher;
    }
    setTimeout(() => {
      this.currentPage++;
      let params = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        rsponUserName: this.userName,
        checkState: null
      };
      if (this.navParams.get("type") == "1") {
        params.checkState = "1";
      } else {
        params.checkState = "0,2";
      }
      this.httpService.queryPlanDetail(params).subscribe(data => {
        if (data.status != 200) {
          let toast = this.commonFunc.showInsideErrorToast(data);
          toast.present();
          return;
        }
        if (data.data.list && data.data.list.length > 0) {
          if (data.data.list) {
            data.data.list.forEach(e => {
              this.list.push(e);
            });
          }
        } else {
          this.refresher.enabled = false;
          this.isShowNoData = true;
        }
      });
      this.refresher.complete();
    }, 500);
  }

  // 维保人员的检查界面
  checkMenu(data){
    this.storage.set("objData",data);

    console.log(data);
      this.navController.push(CheckElevator, {
          planId: data.planId,
          implementObjectId:data.implementObjectId
      });
  }

  detail(p) {
      // 检测前，清空以前作业记录
      this.storage.remove("producePartyResult");
      this.storage.remove("userPartyResult");
      this.storage.remove("elevatorResult");
      this.storage.remove("checkPartyResult");
      this.storage.remove("defineResult");
      this.storage.remove("producePartyStatus");
      this.storage.remove("userPartyStatus");
      this.storage.remove("elevatorStatus");
      this.storage.remove("checkPartyStatus");
      this.storage.remove("definedStatus");
      this.storage.remove("elevatorTotalResult");
      this.storage.remove("safeControl");
      this.storage.remove("safeControl2");

    let param = this.navParams.get("type");
    if (param == "1") {
      //TODO 到详情去
        this.alertCtrl
            .create({
                title: "提示",
                subTitle: "请在电脑端查看详情！",
                buttons: ["确定"]
            })
            .present();
    } else {
      //TODO 弹出开始工作，选择工作类型 检查人员  记录人员
      let alert = this.alertCtrl.create();
      alert.setTitle("请选择你的角色");

      alert.addInput({
        type: "radio",
        label: "我是检查人员",
        value: "1",
        checked: false
      });
      alert.addInput({
        type: "radio",
        label: "我是记录人员",
        value: "2",
        checked: false
      });

      alert.addButton("取消");
      alert.addButton({
        text: "确定",
        handler: data => {
          if (data == "1") {
            //去 检查人员拍照录像界面
            this.navController.push(RecordMedia, {
              pdId: p.id
            });
          } else if (data == "2") {
            //去 记录人员填单界面
            this.navController.push(CheckItem, {
              id: p.plan_dict_id,
              pdId: p.id,
              beCkedPartyName: p.be_cked_party_name,
              beCkedPartyId: p.be_cked_party_id,
              pdType: p.pd_type,
              state: p.check_state,
            });
          } else {
            this.alertCtrl
              .create({
                title: "提示",
                subTitle: "请选择您工作的角色",
                buttons: ["确定"]
              })
              .present();
          }
        }
      });
      alert.present();
    }
  }
}
